$(function () {
    //抽取对象
    var order_dialog = $("#order_dialog");

    $("#orderBill_datagrid").datagrid({
        fitColumns: true,
        url:'/saleOut/query.do',
        toolbar:'#orderBill_toolbar',
        striped: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        columns: [[
            {field: 'sn', title: '单据编码', width: 100},
            {field: 'client', title: '客户', width: 100,formatter:function (value,row,index) {
                    return value ? value.name : "散客";
                }},
            {field: 'depot', title: '仓库', width: 100,formatter:function (value,row,index) {
                    return value ? value.name : "";
                }},
            {field: 'totalNumber', title: '总数', width: 100},
            {field: 'totalAmount', title: '合计', width: 100},
            {field: 'status', title: '状态', width: 100,formatter:function (value, row, index) {
                    return value==2 ? "<font style='color: green'>已审核</font>" : "<font style='color: red'>未审核</font>";
                }},
            {field: 'auditor', title: '审核人', width: 100,formatter:function (value, row, index) {
                    return value? value.username : "";
                }},
            {field: 'auditTime', title: '审核时间', width: 100},
            {field: 'inputUser', title: '录入人', width: 100,formatter:function (value, row, index) {
                    return value? value.username : "";
                }},
            {field: 'inputTime', title: '销售出库时间', width: 100}
        ]]
    })

    order_dialog.dialog({
        width: 700,
        height: 350,
        buttons: '#form_btns',
        closed: true,

    })

    $("#product_view").dialog({
        width: 700,
        height: 420,
        closed: true,
    })

//同一管理执行的方法
    var methodObj = {
        add: function () {
            //打开弹出框
            order_dialog.dialog('open');
            //设置弹出框的标题
            order_dialog.dialog('setTitle', '销售商品');
        },
        //打开关联第二个datagrit
        addProduct: function () {
          $("#product_view").dialog('open')
        },
        //关闭页面
        cancel: function () {
            order_dialog.dialog('close')
        },
        //高级查询
        query:function () {
          var keyword = $("#keyword").textbox('getValue');
          $("#orderBill_datagrid").datagrid('load',{
              keyword:keyword
          })
        },
        //删除明细
        remove:function () {
            var currentTr = $(this).closest("tr");
            currentTr.remove();
        },
        //设置订单审核状态
        audit:function () {
            var row = $("#orderBill_datagrid").datagrid('getSelected')
            if (!row){
                $.messager.alert('温馨提示', '请选中一条数据!', 'warning');
                return;
            }
            $.messager.confirm('提示操作','确认该操作？',function (t) {
                //确认操作
                if (t){
                    $.get("/saleOut/audit.do",{id:row.id},function (data) {
                        if (data.success){
                            $.messager.alert('温馨提示','操作成功','info',function () {
                                $("#orderBill_datagrid").datagrid("reload");
                            })
                        }else {
                            $.messager.alert('温馨提示',data.msg,'error');
                        }
                    },'json')
                }
            })
        },
        //销售出库
        save:function () {
            //index索引     item遍历的对象
            $.each($("#tr_appendTo tr"),function (index,item) {
                $(item).find("[tag=pId]").prop("name","items["+index+"].product.id");
                $(item).find("[tag=totalNumber]").prop("name","items["+index+"].number");
                $(item).find("[tag=salePrice]").prop("name","items["+index+"].salePrice");
            })
            $("#order_form").form('submit',{
                url:'/saleOut/saveOrUpdate.do',
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("温馨提示","操作成功","info",function () {
                            $("#orderBill_datagrid").datagrid("reload");
                            $("#order_dialog").dialog("close")
                        })
                    }else {
                        $.messager.alert("温馨提示","操作失败","error");
                    }
                }
            })
        }
    }
//同一绑定事件
    $("a[data-cmd]").click(function () {
        //获取了当前点击的方法
        var method = $(this).data("cmd");
        //调用同一执行的方法,传入执行的属性
        methodObj[method]();
    });

//显示商品的datagrid
    $("#product_datagrid").datagrid({
        fitColumns: true,
        url: '/product/query.do',
        striped: true,
        rownumbers: true,
        singleSelect: true,
        ctrlSelect: true,
        pagination: true,
        pageNumber: 10,
        scrollbarSize: 0,
        fit: true,

        columns: [[
            {field: 'sn', title: '商品编码', width: 100},
            {field: 'name', title: '商品名称', width: 100},
            {field: 'second', title: '商品分类', width: 100,formatter:function (value, row, index) {
                    return value ? value.classify : "";
                }},
            {field: 'costPrice', title: '零售价', width: 100}
        ]],
        //双击添加商品
        onDblClickRow:function (index,row) {
            var cl = $("#itemTr tr:first").clone();
            cl.appendTo("#tr_appendTo");
            cl.find("span[tag=sn]").html(row.sn);
            cl.find("[tag=pId]").val(row.id);
            cl.find("[tag=productName]").html(row.name);
            cl.find("[tag=salePrice]").val(row.costPrice);
            $("#product_view").dialog('close');
        }
    });
    $("#tr_appendTo").on("change","[tag=totalNumber],[tag=salePrice]",function () {
        var currentTr = $(this).closest('tr');
        var price = currentTr.find("[tag=salePrice]").val() || 0;
        var number = currentTr.find("[tag=totalNumber]").val() || 0;
        currentTr.find("[tag=countPrice]").html((price * number).toFixed(2));
    }).on("click",".del",function () {
        var tr = $(this).closest("tr");
        tr.remove();
    })
})































































